<template>
    <div id="highClassify">
        <div class="thump-container flex-center" ref="thumpContainer">
            <draggable :options="bottomOptions" draggable=".content-img" style="height:100%">
                <img class="thump-img" ref="thumpImg" :src="thumpSrc" alt="">
            </draggable>
            <!-- <draggable class="thump-drag flex-center" :options="bottomOptions" :data-index="-1">
                <img ref="arrangeImg" class="arrange-img" :src="arrangeSrc" alt="">
            </draggable> -->
            <div v-for="(item, index) in hotZone" :key="index">
                <transition name="hotZone">
                    <div class="note-zone" ref="noteZone" v-if="!item.noteZone.isRight"></div>
                </transition>
                <draggable :options="bottomOptions">
                    <div class="areaZone flex-center" ref="areaZone" :data-message="index">
                        <!-- <div v-for="(innerItem, innerIndex) in item.childrenZone" :key="innerIndex" :options="bottomOptions"  :data-index="innerIndex"> -->
                        <!-- </div> -->
                    </div>
                </draggable>
                <div class="hotZone-con" :options="bottomOptions">
                    <!-- <transition-group name="hotZone"> -->
                    <div ref="childrenZone" class="hotZone" :style="innerItem.isRight?'opacity:0':''" v-for="(innerItem, innerIndex) in item.childrenZone" :key="innerIndex" :options="bottomOptions"></div>
                    <!-- </transition-group> -->
                </div>
            </div>
        </div>
        <bottom-handle :noOverflow.sync="noOverflow" :contentNum.sync="contentNum">
            <draggable ref="drag" class="flex-start" v-model="copyContentList" :move="onMove" @end="onEnd" :options="bottomOptions">
                <img @dblclick="bigImg(item)"  class="content-img pointer" :data-index="index" :data-id="item.id" v-for="(item, index) in copyContentList" :key="index" :src="item.src" alt="">
            </draggable>
        </bottom-handle>
    </div>
</template>

<script>
import bottomHandle from '../../components/bottomHandle'
import draggable from "vuedraggable";
import classify from '../../mixin/classify'
import bus from '../../../content/assets/js/bus'
export default {
    components: { draggable, bottomHandle },
    name: "HighClassify",
    mixins: [classify],
    data() {
        return {
            bottomOptions: {
                group: "collection",
                forceFallback: true,
                sort: false,
            },
            noOverflow: false,
            copyContentList: [],
            targetDomMessage: null,
            contentNum: 0
        }
    },
    created() {
        if (this.description.waitingForSelect) {
            this.description.waitingForSelect.map(v => {
                let suffix = this.getFileSuffix(v.content)
                v.src = window.$DATADIR + `resc/Collection/${this.id}/${v.content.replace(/\.(?=[a-z])[a-z]{2,3}[a-z0-9]$/i, suffix)}`
            })
        }
        this.copyContentList = JSON.parse(JSON.stringify(this.description.waitingForSelect))
    },
    computed: {
        thumpSrc() {
            if (this.description.waitingForSelect) {
                let src = this.description.classifyArea[0].localName
                let suffix = this.getFileSuffix(this.description.classifyArea[0].localName)
                return window.$DATADIR + `resc/Collection/${this.id}/${src.replace(/\.(?=[a-z])[a-z]{2,3}[a-z0-9]$/i, suffix)}`
            }
        },
        hotZone() {
            if (this.description.classifyArea) {
                this.description.classifyArea[0].hotZone.map(v => {
                    this.$set(v, 'isRight', false)
                    this.$set(v.noteZone, 'isRight', false)
                    v.childrenZone.map(v => {
                        this.$set(v, 'isRight', false)
                    })
                })
                return this.description.classifyArea[0].hotZone
            }
        }
    },
    mounted() {
        console.log('HighClassify');
        this.$nextTick(() => {
            if (this.noOverflow) {
                this.restDragMaxWidth(this.$refs.drag.$el)
            }
            let time = setInterval(() => {
                try {
                    let rect = this.$refs.thumpImg.getBoundingClientRect()
                    let width = rect.width
                    if (width !== 0) {
                        let height = rect.height
                        let x = rect.x - this.$refs.thumpContainer.getBoundingClientRect().x
                        this.restHotzone(width, height, x)
                        clearInterval(time)
                    }
                } catch (err) {
                    console.log(err)
                    clearInterval(time)
                }
            }, 100)
        })
    },
    methods: {
        bigImg(item){
            console.log('双击',item);
            bus.$emit('showbigImg',item)
        },
        restHotzone(width, height, x) {
            this.$refs.noteZone.map((v, i) => {
                let noteZone = this.hotZone[i].noteZone
                v.style.width = noteZone.width * width + 'px'
                v.style.height = noteZone.height * height + 'px'
                v.style.left = noteZone.pointX * width + x + 'px'
                v.style.top = noteZone.pointY * height + 'px'


            })
            this.$refs.areaZone.map((v, i) => {
                let areaZone = this.hotZone[i]
                v.style.width = areaZone.width * width + 'px'
                v.style.height = areaZone.height * height + 'px'
                v.style.left = areaZone.pointX * width + x + 'px'
                v.style.top = areaZone.pointY * height + 'px'
            })
            let childrenArry = []
            this.hotZone.forEach(v => {
                childrenArry.push(...v.childrenZone)
            })
            childrenArry.forEach((v, i) => {
                let childrenDom = this.$refs.childrenZone[i]
                childrenDom.style.width = v.width * width + 'px'
                childrenDom.style.height = v.height * height + 'px'
                childrenDom.style.left = v.pointX * width + x + 'px'
                childrenDom.style.top = v.pointY * height + 'px'
            })
        },
        onMove(customEvent) {
            console.log(customEvent)
            this.targetDomMessage = customEvent.related.dataset.message ? customEvent.related.dataset.message : null
            return false
        },
        onEnd(customEvent) {
            console.log(this.targetDomMessage)
            if (this.targetDomMessage === null) return
            let answerMessage = parseInt(this.targetDomMessage)
            let answerArry = this.hotZone[answerMessage].childrenZone
            let answerId = customEvent.item.dataset.id
            let answerIndex = parseInt(customEvent.item.dataset.index)
            let index = -1
            let isBelog = answerArry.some((v, i) => {
                index = i
                return v.answer === answerId
            })
            if (isBelog) {
                this.hotZone[answerMessage].childrenZone[index].isRight = true
                this.copyContentList.splice(answerIndex, 1)
                this.hotZone[answerMessage].noteZone.isRight = this.hotZone[answerMessage].childrenZone.every(v => {
                    return v.isRight === true
                })
            }
            if (!this.noOverflow) {
                if (this.copyimgList.length <= this.contentNum) {
                    this.noOverflow = true
                }
            }
            if (this.noOverflow) {
                this.$nextTick(() => {
                    this.restDragMaxWidth(this.$refs.drag.$el)
                })
            }
            this.targetDomMessage = null
        },
    },
}
</script>

<style lang="scss">
.hotZone-leave-active {
    transition: opacity 1s;
}
.hotZone-enter,
.hotZone-leave-to {
    opacity: 0;
}
#highClassify {
    .thump-container {
        font-size: 16px;
        height:33.6vw;
        width: 77.78vw;
        margin: 0 auto;
        margin-bottom: 25px;
        background: #fff;
        border-radius: 10px;
        position: relative;
        .thump-img {
            border-radius: 10px;
            height: 100%;
            // width: 100%;
        }
        // .thump-drag {
        //     width: 100%;
        // }
        // .arrange-img {
        //     height: 100%;
        //     border-radius: 37px;
        // }
        // .hotZone-con {
        //     position: absolute;
        //     border-radius: 8px;
        // }
        .note-zone {
            position: absolute;
            background-color: #fff;
        }
        .areaZone {
            position: absolute;
            background-color: transparent;
            z-index: 100;
        }
        .hotZone {
            position: absolute;
            background-color: #fff;
            transition: all 0.8s ease;
            z-index: 50;
        }
        .conceal {
            opacity: 1;
        }
    }
    .content-img {
        margin-bottom: 5px;
        border-radius: 10px;
        width: 12.5vw;
        height: 12.5vw;
        min-width: 12.5vw;
        min-height: 12.5vw;
        max-width: 12.5vw;
        max-height: 12.5vw;
        &:not(:last-child) {
            margin-right: 30px;
        }
    }
    @media screen and(max-width: 1439px) {
        .content-img {
            margin-bottom: 5px;
            border-radius: 10px;
            width: 10vw;
            height: 10vw;
            min-width: 10vw;
            min-height: 10vw;
            max-width: 10vw;
            max-height: 10vw;
            &:not(:last-child) {
                margin-right: 20px;
            }
        }
        .drag-list {
            height: 12.4vw;
            .icon {
                width: 3vw;
                height: 3vw;
            }
        }
    }
}

</style>
